<?php

declare(strict_types=1);
/**
 * @Create by PhpStorm
 * @author:jinxiu89@163.com
 * @Create Date:2020/7/10 10:15
 * @User: admin
 * @Current File : Category.php
 * @格言：溪涧岂能留得住，终归大海做波涛 --李忱
 * @格言： 我的内心因看见大海而波涛汹涌
 **/

namespace app\frontend\service;

use app\common\model\mysql\category\ProductCategory as model;
use app\libs\utils\Category as Helper;
use Exception;
use think\facade\Db;

/**
 * Class Category
 * @package app\frontend\service
 */
class Category extends BaseService
{
    /**
     * Category constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->model = new model();
    }

    /**
     * @param int $language
     */
    public function getDataByLanguage(int $language)
    {
        try {
            $obj = $this->model::getDataByLanguage((int)$status = 1, (int)$language)->toArray();
            return Helper::toLayer((array)$obj, (string)'child', (int)0);
        } catch (\Exception $exception) {
            return [];
        }
    }

    public function getProductByCategory(int $status, int $language)
    {

        try {
            $obj = $this->model::getProductByCategory((int) $status, (int) $language);
            return Helper::toLayer((array) $obj, (string)'child', (int)0);
        } catch (Exception $exception) {
            return $exception->getMessage();
        }
    }
    public function getCategoryDate(int $status, int $language,$level){
        //var_dump($status,$language);exit();
        $where = [
            'status'=>$status,
            'language_id'=>$language,
            'level'=>$level
        ];
        $data = Db::table("tb_product_category")
              -> where($where)
              ->select()
              ->toArray();
        if (!empty($data)){
            return $data;
        }else{
            return "获取分类失败";
        }
    }

    public function ProductDateByCate($status, $language,$category_id){
            $where = [
                'status'=>$status,
                'language_id'=>$language,
            ];
            $category_id = intval($category_id);
            $where1 = "FIND_IN_SET($category_id,path)";

                $cateDate = Db::table('tb_product_category')
                    ->where($where)
                    ->where($where1)
                    ->select()
                    ->toArray();
                //var_dump($cateDate);exit();
                $ids = array();
                if (empty($cateDate)){
                    $ids[] = $category_id;
                }else{
                    $ids = array_column($cateDate, 'id');
                }
                //var_dump($ids);exit();
                $sql = '';
                foreach($ids as $k => $id){
                    if($k == 0){
                        $sql .= "FIND_IN_SET( $id, category_id )";
                    }else{
                        $sql .= " or FIND_IN_SET( $id, category_id )";
                    }
                }
                $productData = Db::table('tb_product')
                    ->where($sql)
                    ->paginate(15);
                    //->select()
                    //->toArray();
                //判断是否为顶级分类，如果是则只显示下一级分类
                $level = $ids = array_column($cateDate, 'level');
                if (in_array(1,$level)){
                    $categoryData = array();
                    foreach ($cateDate as $value){
                        if ($value['level']==1){
                            $categoryData[] = $value;
                        }
                    }
                    $cateDate = $categoryData;
                }
                //var_dump($cateDate);exit();
                if (!empty($productData)){
                    $res = array();
                    $res[] = $cateDate;
                    $res[] = $productData;
                    //var_dump($res);exit();
                    return $res ;
                }else{
                    return "产品不存在";
                }


    }

    /**
     * @param $status
     * @param $language
     * @param $house_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function ProductDateByHouse($status,$language,$house_id){
        $where = [
            'status'=>$status,
            'language_id'=>$language,
        ];
        $house_id = intval($house_id);
        $where1 = "FIND_IN_SET($house_id,path)";
        if ($house_id){
            $cateData = Db::table('tb_product_category')
                      ->where($where)
                      ->where($where1)
                      ->select()
                      ->toArray();
            $houseList = array();
            if (!empty($cateData)){
                /*foreach ($cateData as $value){
                    if ($value['level']==1){
                        $houseList[] = $value;
                    }
                }*/
                $house_id = 1;
            }
            $where2 = "FIND_IN_SET($house_id,contact_tag)";
            $productData = Db::table('tb_product')
                  ->where($where)
                  ->where($where2)
                  ->select()
                  ->toArray();
            if (!empty($productData)){
                $listdata = array();
                $listdata[] = $cateData;
                $listdata[] = $productData;

                return $listdata;
            }

        }

    }

    public function cateidByname($status, $language,$category_name){
        $where = [
            'status'=>$status,
            'language_id'=>$language,
        ];
        $data = Db::table('tb_product_category')
              ->field('id')
              ->where($where)
              ->where("name LIKE '".$category_name."%'")
              ->find();
        //var_dump(Db::table('tb_product_category')->getLastSql());exit();
        if ($data){
            return $data;
        }

    }

    public function getPath($status, $language,$category_id){
        $category_id = intval($category_id);
        $where = [
            'status'=>$status,
            'language_id'=>$language,
            'id'=>$category_id,
        ];

        $path =Db::table('tb_product_category')
              ->field('path')
              ->where($where)
              ->find();
        $path_arr = array();
        $path_arr = explode(',',$path['path']);
        $path_arr[] = $category_id;
        $data =Db::table('tb_product_category')
            ->field("id,name,is_parent")
            ->where('id', 'in', $path_arr)
            ->select()
            ->toArray();
        if ($data) {
            return $data;
        }
    }

    function cateGettree($array, $pid = 0){
        static $list = [];
        foreach ($array as $key => $value) {
            if ($value['parent_id'] == $pid) {
                $list[] = $value;
                $this->cateGettree($array, $value['id']);

            }
        }
        return $list;
    }
}